package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyExperienceActiviteAdapter;
import com.kkd.kkdapp.adapter.MyTiyanAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyExperienceColdData;
import com.kkd.kkdapp.http.entity.MyExperienceGoldActiveItem;
import com.kkd.kkdapp.http.entity.TenderDetailMapList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyExperienceGoldActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private Context mContext = this;

	private static final int SUCCESS_REQUESTEXPERIENCEGOLD = 0;
	private static final int FAIL_REQUESTEXPERIENCEGOLD = 1;

	private RelativeLayout rel_top_back_myEeperienceGold;
	private KkdXlistView mXlistView;
	private TextView num_canUse_tv_head_myExperienceGold;
	private RelativeLayout toDetail_linear_head_myExperienceGold;
	private TextView num_shouyi_tv_head_myExperienceGold;
	private ImageView iv_rel_head_myExperienceGold;
	private ListView mListView;
	private List<TenderDetailMapList> list_myExperienceGoldItem = new ArrayList<TenderDetailMapList>();
	private List<MyExperienceGoldActiveItem> list_myExperienceGoldActiveItem = new ArrayList<MyExperienceGoldActiveItem>();
	private DecimalFormat df = new DecimalFormat("#,###.00");
	private DecimalFormat df2 = new DecimalFormat("#,###");
	private MyTiyanAdapter mAdapter;
	private MyExperienceActiviteAdapter mAdapter_active;
	private Dialog mDialog;
	private int isRealNameAuth;
	private int isHaveBinCard;
	private ImageView image_null;
	private TextView tv_null;
	private boolean isShowToastOnRefresh = false;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCCESS_REQUESTEXPERIENCEGOLD:
				if (mAdapter == null) {
					mAdapter = new MyTiyanAdapter(mContext,
							list_myExperienceGoldItem);
					mListView.setAdapter(mAdapter);
				}
				MyExperienceColdData data = (MyExperienceColdData) msg.obj;
				// 可用特权本金
				long experAmount = data.getExperAmount();
				num_canUse_tv_head_myExperienceGold.setText(df2
						.format((experAmount / 100)));
				// 已收益
				double experIncome = data.getExperIncome() / (double) 100;
				num_shouyi_tv_head_myExperienceGold.setText(df.format(
						experIncome).startsWith(".") ? "0"
						+ df.format(experIncome) : df.format(experIncome));
				// 是否已实名
				isRealNameAuth = data.getIsRealNameAuth();
				// 是否已绑卡
				isHaveBinCard = data.getIsHaveBinCard();
				// 展示的图片url
				String jsImageUrl = data.getJsImageUrl();
				if (TextUtils.isEmpty(jsImageUrl)) {
					iv_rel_head_myExperienceGold.setVisibility(View.GONE);
				} else {
					iv_rel_head_myExperienceGold.setVisibility(View.VISIBLE);
					ImageLoader.getInstance().displayImage(jsImageUrl,
							iv_rel_head_myExperienceGold, R.drawable.home_ad);
					iv_rel_head_myExperienceGold
							.setOnClickListener(new OnSingleClickListener() {

								@Override
								public void onSingleClick(View v) {
									if (isRealNameAuth == 0) {
										Intent intent = new Intent(mContext,
												ForTenderActivity.class);
										intent.putExtra("enterTypeFlag", 0);
										startActivity(intent);
									} else if (isHaveBinCard == 0) {
										Intent intent = new Intent(mContext,
												ForTenderActivity.class);
										intent.putExtra("enterTypeFlag", 0);
										startActivity(intent);
									}
								}
							});
				}
				// 特权本金活动
				list_myExperienceGoldActiveItem = data.getExperActivity();
				if (mAdapter_active == null) {
					mAdapter_active = new MyExperienceActiviteAdapter(mContext,
							list_myExperienceGoldActiveItem);
					lv_head_myExperienceGold.setAdapter(mAdapter_active);
				}
				// 特权本金条目
				list_myExperienceGoldItem.clear();
				if (data.getDataList() != null && data.getDataList().size() > 0) {
					list_myExperienceGoldItem.addAll(data.getDataList());
					rel_tip_null.setVisibility(View.GONE);
				} else {
					rel_tip_null.setVisibility(View.VISIBLE);
				}
				mAdapter.notifyDataSetChanged();
				mXlistView.stopLoadMore(1);
				mXlistView.stopRefresh();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				mXlistView.setRefreshTime(time);

				image_null.setVisibility(View.GONE);
				tv_null.setVisibility(View.GONE);

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;

			case FAIL_REQUESTEXPERIENCEGOLD:
				mXlistView.stopLoadMore(1);
				mXlistView.stopRefresh();

				if (mAdapter == null) {
					image_null.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						tv_null.setText("网络错误，请稍候下拉刷新重试");
					} else {
						tv_null.setText("服务异常，请稍候下拉刷新重试");
					}
					image_null.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.VISIBLE);
				}

				if (isShowToastOnRefresh) {
					String desc = (String) msg.obj;
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;

			default:
				break;
			}
		}
	};

	private ListView lv_head_myExperienceGold;

	private View rel_tip_null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_experience_gold);
		initView();
		initListener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		requestDataFromWeb();
	}

	private void initView() {
		rel_top_back_myEeperienceGold = (RelativeLayout) findViewById(R.id.rel_top_back_myEeperienceGold);
		mXlistView = (KkdXlistView) findViewById(R.id.kkdXlistView_myEeperienceGold);
		image_null = (ImageView) findViewById(R.id.id_no_content);
		tv_null = (TextView) findViewById(R.id.id_no_content_text);
		/*
		 * 初始化头部view
		 */
		View headView = LayoutInflater.from(mContext).inflate(
				R.layout.head_my_experience_gold, null);
		// 可用特权本金
		num_canUse_tv_head_myExperienceGold = (TextView) headView
				.findViewById(R.id.num_canUse_tv_head_myExperienceGold);
		// 查看明细
		toDetail_linear_head_myExperienceGold = (RelativeLayout) headView
				.findViewById(R.id.check_new);
		// 已经收益
		num_shouyi_tv_head_myExperienceGold = (TextView) headView
				.findViewById(R.id.num_shouyi_tv_head_myExperienceGold);
		// tip
		iv_rel_head_myExperienceGold = (ImageView) headView
				.findViewById(R.id.iv_rel_head_myExperienceGold);
		// 特权本金活动
		lv_head_myExperienceGold = (ListView) headView
				.findViewById(R.id.lv_head_myExperienceGold);
		// 无记录提示
		rel_tip_null = headView.findViewById(R.id.rel_tip_null);
		/*
		 * 初始化mXlistView
		 */
		mXlistView.setKKDScrollListener(this);
		mListView = mXlistView.mListView;
		mListView.setDividerHeight(0);
		mListView.addHeaderView(headView);

		mXlistView.startRefresh();
	}

	private void initListener() {
		rel_top_back_myEeperienceGold.setOnClickListener(this);
		toDetail_linear_head_myExperienceGold.setOnClickListener(this);
		findViewById(R.id.what).setOnClickListener(this);
	}

	/*
	 * 请求特权本金接口
	 */
	private void requestDataFromWeb() {
		String customerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(mContext);
		final String[] cat = customerIdAndToken.split("&");
		if (cat == null || cat.length < 2)
			return;
		KkdHttpMagager.getInstance().myExperienceCold(
				MyExperienceColdData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyExperienceColdData data = (MyExperienceColdData) responseBody;
						Message msg = Message.obtain();
						msg.what = SUCCESS_REQUESTEXPERIENCEGOLD;
						msg.obj = data;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_REQUESTEXPERIENCEGOLD;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rel_top_back_myEeperienceGold:
			finish();
			break;

		case R.id.what:
			SystemUtils.onEvent("Account_ExperienceMoney_what", 1);
			startActivity(new Intent(mContext, HowToExperienceActivity.class));
			break;

		case R.id.check_new:
			SystemUtils.onEvent("Account_ExperienceMoney_Detail", 1);
			Intent intent = new Intent(mContext,
					ExperienceGoldDetailActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		requestDataFromWeb();
	}

	@Override
	public void onLoadMore() {
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				mXlistView.stopLoadMore(-1);
				mXlistView.stopRefresh();
				mXlistView.setNoMore(false);
			}
		});
	}

}
